﻿namespace eProject.ViewModel.Commands
{
    public class ChangeEmployeeCommand: ICommand
    {
        private readonly CommonManager _manager;

        private bool _isCanceled;
        private bool _isExecuted;
        private readonly string _oldName;
        private readonly string _oldSurname;
        private readonly string _oldEmail;
        private readonly string _oldPhonenumber;
        private readonly string _newName;
        private readonly string _newSurname;
        private readonly string _newEmail;
        private readonly string _newPhonenumber;
        

        public ChangeEmployeeCommand(CommonManager manager, EmployeeViewModel employee, string newName, string newSurname, string newPhonenumber, string newEmail)
        {
            _manager = manager;
            _oldName = employee.Name;
            _oldSurname = employee.Surname;
            _oldPhonenumber = employee.PhoneNumber;
            _oldEmail = employee.EMail;
            _newName = newName;
            _newSurname = newSurname;
            _newPhonenumber = newPhonenumber;
            _newEmail = newEmail;
        }       

        public void Execute()
        {
            if (_isExecuted) return;
            var employee = _manager.GetEmployee(_oldEmail);
            employee.Name = _newName;
            employee.Surname = _newSurname;
            employee.EMail = _newEmail;
            employee.PhoneNumber = _newPhonenumber;
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;
            
            var employee = _manager.GetEmployee(_newEmail);            
            employee.Name = _oldName;
            employee.Surname = _oldSurname;
            employee.EMail = _oldEmail;
            employee.PhoneNumber = _oldPhonenumber;

            _isCanceled = true;
            _isExecuted = false;
        }
    }
}
